package com.itxiuyixiu.homework.admin.controller;

import com.itxiuyixiu.homework.admin.service.AdminCourseService;
import com.itxiuyixiu.homework.entity.dto.CourseDto;
import com.itxiuyixiu.homework.entity.dto.CourseRecordDto;
import com.itxiuyixiu.homework.entity.model.CourseRecord;
import com.itxiuyixiu.homework.entity.vo.CourseRecordVo;
import com.itxiuyixiu.homework.entity.vo.CourseVo;
import com.itxiuyixiu.homework.entity.vo.StudentVo;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnMessage;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 黄磊
 * @date 2020/3/9
 */
@Api(tags = "管理员-课程管理系统")
@RestController
public class AdminCourseController {
    @Autowired
    private AdminCourseService adminCourseService;

    @ApiOperation(value = "获取课程信息" , response = CourseVo.class , responseContainer = "List", notes = "使用id、courseName、majorId、teacherId来进行查询。其他字段无效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "课程id" , dataType = "int"),
            @ApiImplicitParam(name = "courseName" , value = "课程名字"),
            @ApiImplicitParam(name = "majorId" , value = "专业id" , dataType = "int"),
            @ApiImplicitParam(name = "teacherId" , value = "老师id" , dataType = "int")
    })
    @GetMapping("/admin/course/message")
    public ReturnVo findCourseMessage(CourseDto dto) {
        return ReturnMessage.getSuccessReturn(adminCourseService.findCourseMessage(dto));
    }

    @ApiOperation(value = "添加课程信息" , response = String.class)
    @PostMapping("/admin/course/message")
    public ReturnVo addCourseMessage(CourseDto dto) {
        switch (adminCourseService.addCourseMessage(dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功添加课程");
            case 1:
                return ReturnMessage.getFailureReturn("老师不存在");
            case 2:
                return ReturnMessage.getFailureReturn("专业不存在");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知情况");
        }
    }

    @ApiOperation(value = "修改课程信息" , response = String.class)
    @PutMapping("/admin/course/message")
    public ReturnVo modifyCourseMessage(int courseId, CourseDto dto) {
        switch (adminCourseService.modifyCourseMessage(courseId, dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功修改课程信息");
            case 1:
                return ReturnMessage.getFailureReturn("课程不存在");
            case 2:
                return ReturnMessage.getFailureReturn("老师不存在");
            case 3:
                return ReturnMessage.getFailureReturn("专业不存在");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知情况");
        }
    }

    @ApiOperation(value = "删除课程" , response = String.class)
    @DeleteMapping("/admin/course/message")
    public ReturnVo deleteCourse(int courseId) {
        switch (adminCourseService.deleteCourse(courseId)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功删除课程信息");
            case 1:
                return ReturnMessage.getFailureReturn("课程不存在");
            case 2:
                return ReturnMessage.getFailureReturn("存在相应的课程情况");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知情况");
        }
    }

    @ApiOperation(value = "学生课程情况查询" , response = CourseRecordVo.class , notes = "只有id、courseId、studentId有用，其他属性无效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "课程情况id" , dataType = "int"),
            @ApiImplicitParam(name = "courseId" , value = "课程id" , dataType = "int"),
            @ApiImplicitParam(name = "studentId" , value = "学号"),
    })
    @GetMapping("/admin/student/course")
    public ReturnVo findCourseRecordMessge(CourseRecordDto dto) {
        return ReturnMessage.getSuccessReturn(adminCourseService.findCourseRecordMessage(dto));
    }

    @PostMapping("/admin/student/course")
    public ReturnVo addCourseRecordMessge(CourseRecordDto dto) {
        switch (adminCourseService.addCourseRecordMessage(dto)){
            case 0: return ReturnMessage.getSuccessReturn("添加课程成功");
            case 1: return ReturnMessage.getFailureReturn("课程不存在，请先添加课程");
            case 2: return ReturnMessage.getSuccessReturn("学生不存在，请先添加学生");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知情况");
        }
    }

    @ApiOperation(value = "学生退课" , response = String.class)
    @DeleteMapping("/admin/student/course")
    public ReturnVo withdrawCourse(int courseRecordId) {
        switch (adminCourseService.withdrawCourse(courseRecordId)) {
            case 0:
                return ReturnMessage.getSuccessReturn("退课成功");
            case 1:
                return ReturnMessage.getFailureReturn("输入ID号错误");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知情况");
        }
    }

    @ApiOperation(value = "上传成绩" , response = String.class)
    @PostMapping("/admin/student/score")
    public ReturnVo uploadScore(String studentId , int courseId , int score , boolean isMakeup) {
        switch (adminCourseService.uploadScore(studentId, courseId, score, isMakeup)) {
            case 0:
                return ReturnMessage.getSuccessReturn("成功上传成绩");
            case 1:
                return ReturnMessage.getFailureReturn("学生不存在");
            case 2:
                return ReturnMessage.getFailureReturn("课程不存在");
            case 3:
                return ReturnMessage.getFailureReturn("课程记录不存在");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知情况");
        }
    }
}
